import time,random
from common.login import Login
from common.report_generate import Report

#首页上方快捷导航栏功能验证
class Navigation:
    def __init__(self):
        self.st = Login()
        self.driver = self.st.driver
        self.insert = Report()
    def navigation(self):
        self.st.login_fast('超级管理员')

        #遍历上方快捷导航栏，获取text并点击链接跳转
        for i in range(2,11):
            xpath = '//*[@id="content"]/div[2]/div[1]/div[%d]/div/div/div' % i
            text = self.driver.find_element_by_xpath(xpath).text

            #就业管理和市场营销模块与左侧导航栏link_text定位方式相同，所以通过xpath进行定位
            if text == '就业管理':
                self.driver.find_element_by_xpath('//*[@id="content"]/div[2]/div[1]/div[4]/div/div/div/a').click()
                time.sleep(2)
            elif text == '市场营销':
                self.driver.find_element_by_xpath('//*[@id="content"]/div[2]/div[1]/div[5]/div/div/div/a').click()
                time.sleep(2)
            else:
                self.driver.find_element_by_link_text(text).click()
                time.sleep(2)
             #断言 通过获取result（界面title）与text进行对比（是否相同）
             #---其中：学员信息对应基本信息，市场营销对应简历资源，申请加班对应加班申请，所以先进行if判断
            try:
                result = self.driver.find_element_by_xpath('//*[@id="con-hender-nav"]/p').text
                #print(result)
                if result == text:
                    print(f'{text}跳转成功')
                    self.driver.find_element_by_xpath('/html/body/div[2]/div[1]/a/img').click()
                    self.insert.write_report('3.5','首页','GUI测试','TC-020',text + '导航栏跳转','成功','无','无')
                    time.sleep(2)
                elif result == '基本信息':
                    print(f'{text}跳转成功')
                    self.driver.find_element_by_xpath('/html/body/div[2]/div[1]/a/img').click()
                    self.insert.write_report('3.5', '首页', 'GUI测试', 'TC-020',text +'导航栏跳转' , '成功', '无', '无')
                    time.sleep(2)
                elif result == '简历资源':
                    print(f'{text}跳转成功')
                    self.driver.find_element_by_xpath('/html/body/div[2]/div[1]/a/img').click()
                    self.insert.write_report('3.5', '首页', 'GUI测试', 'TC-020', text + '导航栏跳转', '成功', '无', '无')
                    time.sleep(2)
                elif result == '加班申请':
                    print(f'{text}跳转成功')
                    self.driver.find_element_by_xpath('/html/body/div[2]/div[1]/a/img').click()
                    self.insert.write_report('3.5', '首页', 'GUI测试', 'TC-020', text + '导航栏跳转', '成功', '无', '无')
                    time.sleep(2)
                else:
                    print(f'{text}跳转错误')
                    img = self.insert.pic_screenshot()
                    self.insert.write_report('3.5', '首页', 'GUI测试', 'TC-020', text + '导航栏跳转', '错误', '无',img)
                    self.driver.back()
                    time.sleep(2)
            except:
                print(f'{text}跳转失败')
                img = self.insert.pic_screenshot()
                self.insert.write_report('3.5', '首页', 'GUI测试', 'TC-020', text + '导航栏跳转', '失败', '无', img)
                self.driver.back()
                time.sleep(2)



    def notice(self):
        self.st.login_fast('超级管理员')
        self.st.open_module('系统管理', '公告发布')
        self.st.Decrypt()
        num = random.randint(1,1000)
        strs = 'test' + str(num)
        #print(strs)
        self.driver.find_element_by_id('notice-title').send_keys('test' + str(num))
        self.driver.find_element_by_xpath("//div[starts-with(@id,'text-elem')]").send_keys('test-content' + str(num))
        self.driver.find_element_by_xpath('//*[@id="content"]/div[2]/div[4]/button').click()
        time.sleep(2)
        result = self.driver.find_element_by_xpath('/html/body/div[8]/div/div/div[2]/div').text
        if result == '新增成功':
            self.driver.find_element_by_xpath('/html/body/div[8]/div/div/div[3]/button').click()
            time.sleep(1)
            self.driver.find_element_by_xpath('/html/body/div[2]/div[1]/a/img').click()
            time.sleep(3)
            text = self.driver.find_element_by_xpath('//*[@id="content"]/div[2]/div[2]/div/div[2]/div[1]/a[1]').text
            if text == '○ 【通知公告】' + strs:
                print('公告发布成功，排序方式正确')
                self.insert.write_report('3.5', '首页', 'GUI测试', 'TC-021', '公告栏排序', '成功', '无', '无')
            else:
                print('公告发布成功，排序方式错误')
                img = self.insert.pic_screenshot()
                self.insert.write_report('3.5', '首页', 'GUI测试', 'TC-021', '公告栏排序', '失败', '无', img)
        else:
            print('公告发布失败')
            img = self.insert.pic_screenshot()
            self.insert.write_report('3.5', '首页', 'GUI测试', 'TC-022', '公告发布', '失败', '无', img)







if __name__ == '__main__':
    class1 = Navigation()
    class1.notice()